using System;
using System.Text;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NBear.Common;
using NBear.Data;

namespace NBear.Test.UnitTests
{
    [Table("TempTable")]
    public interface TempTable3 : IEntity
    {
        [PrimaryKey]
        Int64 ID { get; }
        string ColName { get; set; }
    }

    [Table("select `t1`.`ID`, `t1`.`ColName` from `TempTable` `t1` inner join `TempTable` `t2` on `t1`.`ID` = `t2`.`ID`", IsView = true)]
    public interface TempTableJoin2 : IEntity
    {
        [PrimaryKey]
        Int64 ID { get; set; }
        string ColName { get; set; }
    }

    /// <summary>
    /// Summary description for TestMySqlDbProvider
    /// </summary>
    [TestClass]
    public class TestMySqlDbProvider
    {
        public TestMySqlDbProvider()
        {
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        [ClassInitialize()]
        public static void MyClassInitialize(TestContext testContext)
        {
            Gateway.Default = new Gateway(DatabaseType.MySql,
                "Dsn=mysqltest;database=test;option=3;server=localhost;uid=root;password=sa");
        }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        //[TestMethod]
        public void TestMySqlDbProviderGateway()
        {
            TempTable3[] objs = Gateway.Default.SelectAll<TempTable3>();

            TempTable3[] obj2s = Gateway.Default.Select<TempTable3>(5, 2, "ID < ?ID", "ID desc", new object[] { 5 });

            TempTableJoin2[] objs3 = Gateway.Default.SelectAll<TempTableJoin2>();
            objs3 = Gateway.Default.Select<TempTableJoin2>(2, 2);

            TempTable3 obj = EntityFactory<TempTable3>.CreateObject();
            obj.ColName = Guid.NewGuid().ToString();

            System.Data.Common.DbTransaction tran = Gateway.Default.BeginTransaction();

            Gateway.Default.Insert<TempTable3>(obj, tran);

            obj.ColName = Guid.NewGuid().ToString();
            Gateway.Default.Update<TempTable3>(obj, tran);

            Gateway.Default.Delete<TempTable3>("ID = ?ID", new object[] { 1 }, tran);

            tran.Commit();
        }
    }
}
